package com.lyf.shop.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.lyf.shop.constant.LoginConstant;
import com.lyf.shop.constant.OrderConstant;
import com.lyf.shop.entity.Order;
import com.lyf.shop.dao.OrderDao;
import com.lyf.shop.entity.User;
import com.lyf.shop.service.CarService;
import com.lyf.shop.service.OrderService;
import com.lyf.shop.vo.CartVo;
import com.lyf.shop.vo.OrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * (Order)表服务实现类
 *
 * @author makejava
 * @since 2021-12-03 14:46:13
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;
    @Autowired
    private HttpSession httpSession;
    @Autowired
    private CarService carService;
    /**
     * 通过ID查询单条数据
     *
     * @param oid 主键
     * @return 实例对象
     */
    @Override
    public Order queryById(Integer oid) {
        return this.orderDao.queryById(oid);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Order> queryAllByLimit(int offset, int limit) {
        return this.orderDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Override
    public Order insert(Order order) {
        this.orderDao.insert(order);
        return order;
    }

    /**
     * 修改数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Override
    public Order update(Order order) {
        this.orderDao.update(order);
        return this.queryById(order.getOid());
    }

    /**
     * 通过主键删除数据
     *
     * @param oid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer oid) {
        return this.orderDao.deleteById(oid) > 0;
    }

    @Override
    public Order createOrder() throws JsonProcessingException {
        Order order = new Order();
        order.setStatus(OrderConstant.NO_PAY);
        User member = (User) httpSession.getAttribute(LoginConstant.LOGIN_USER_KEY);
        order.setUid(member.getUid());
        order.setAddTime(new Date());
        CartVo cart = carService.getCart();
        order.setTotal(cart.getTotalPrice());
        orderDao.insert(order);
        return order;
    }

    @Override
    public OrderVo createOrderVo(Order order) throws JsonProcessingException {
        OrderVo orderVo = new OrderVo();
        BeanUtils.copyProperties(order,orderVo);
        User user = (User) httpSession.getAttribute(LoginConstant.LOGIN_USER_KEY);
        orderVo.setUser(user);
        CartVo cart = carService.getCart();
        orderVo.setNum(cart.getTotalCount());
        return orderVo;
    }

    @Override
    public List<Order> queryAll(Order order) {
        return this.orderDao.queryAll(order);
    }
}